package com.task.mall.dao.impl;

import com.task.mall.bean.Login;
import com.task.mall.bean.Password;
import com.task.mall.bean.User;
import com.task.mall.dao.UserDao;
import com.task.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao {
    QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());

    @Override
    public List<User> queryAllUser() {

        List<User> userList = null;
        try {
            userList = runner.query("select * from user",
                    new BeanListHandler<>(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }

    @Override
    public int deleteUser(String id) {
        int update = 0;
        try {
            update = runner.update("delete from user where id = ?",
                    id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (update != 0){
            return 200;
        }
        return 10000;
    }

    @Override
    public List<User> searchUser(String word) {
        String likeNickname = "%" + word + "%";
        List<User> userList = null;
        try {
            userList = runner.query("select * from user where nickname like ?",
                    new BeanListHandler<>(User.class),
                    likeNickname);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }

    @Override
    public int login(User user) {
        User id = null;
        try {
            id = runner.query("select * from user where email = ? and pwd = ?",
                    new BeanHandler<>(User.class),
                    user.getEmail(),
                    user.getPwd());
        } catch (SQLException e) {
            e.printStackTrace();
            return 500;
        }
        if (id != null) {
            return 200;
        }
        return 404;
    }

    @Override
    public Login getLoginInfo(String email) {
        Login loginInfo = null;
        try {
            loginInfo = runner.query("select nickName from user where email = ?",
                    new BeanHandler<>(Login.class),
                    email);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return loginInfo;
    }

    @Override
    public int signup(User user) {
        int update = 0;
        try {
            update = runner.update("insert into user values (null, ?, ?, ?, ?, ?, ?)",
                    user.getEmail(),
                    user.getNickname(),
                    user.getPwd(),
                    user.getRecipient(),
                    user.getAddress(),
                    user.getPhone());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return update;
    }

    /**
     * 获取用户信息用来显示以便后续修改
     * @param token
     * @return
     */
    @Override
    public User getUserData(String token) {
        User user = null;
        try {
            user = runner.query("select * from user where nickName = ?",
                    new BeanHandler<>(User.class),
                    token);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    /**
     * 修改用户密码
     * @param password
     * @return
     */
    @Override
    public int changePwd(Password password) {
        try {
            runner.update("update user set pwd = ? where id = ?",
                    password.getConfirmPwd(),
                    password.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 200;
    }

    @Override
    public int updateUserData(User user) {
        try {
            int update = runner.update("update user set nickname = ?, name = ?, address = ?, phone = ? where id = ?",
                    user.getNickname(),
                    user.getRecipient(),
                    user.getAddress(),
                    user.getPhone(),
                    user.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 200;
    }


}
